package com.tuanchauict.android.englishtip.widget;

import java.util.Timer;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.tuanchauict.android.englishtip.R;
import com.tuanchauict.android.englishtip.activities.DictManager;
import com.tuanchauict.android.englishtip.timer.UpdateTask;

public class Widget extends AppWidgetProvider {

	public static final String ACTION_WIDGET_MANAGER = "DictionaryManager";
	public static final String ACTION_WIDGET_SOUND = "TextToSpeech";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		initClick(context, appWidgetManager, appWidgetIds);
		
		// TODO: run swap word
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new UpdateTask(context, appWidgetManager), 1,
				1000);

	}

	private void initClick(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		RemoteViews remotes = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		Intent mngIntent = new Intent(context, DictManager.class);
		mngIntent.setAction(ACTION_WIDGET_MANAGER);

		Intent speechIntent = new Intent(context, Widget.class);
		speechIntent.setAction(ACTION_WIDGET_SOUND);
		// TODO: get current word
		speechIntent.putExtra("text", "demo");

		PendingIntent mngPendingIntent = PendingIntent.getBroadcast(context, 0,
				mngIntent, 0);
		PendingIntent spePendingIntent = PendingIntent.getActivity(context, 0,
				speechIntent, 0);

		remotes.setOnClickPendingIntent(R.id.widget_view_word, spePendingIntent);
		remotes.setOnClickPendingIntent(R.id.widget_view_option,
				mngPendingIntent);

		appWidgetManager.updateAppWidget(appWidgetIds, remotes);
	}
	
	public void  onRecieve(Context context, Intent intent) {
		
	}
}
